package com.ftcs.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftcs.entity.*;
import com.ftcs.mapper.PermissionMapper;
import com.ftcs.mapper.RoleMapper;
import com.ftcs.mapper.UserMapper;
import com.ftcs.mapper.UserRoleMapper;
import com.ftcs.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
//@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 5)
public class SystemServiceImpl implements SystemService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    private SystemService systemservice;

    @Override
    public List<User> getAllUsers() {
        return userMapper.getUserList(null);
    }

    @Override
    public List<User> getPageUsers(int current, int pageSize) {
        Page<User> page = new Page(current, pageSize);
        return userMapper.getUserList(page);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.getUserByid(id);
    }

    @Override
    public User getUserByUserName(String userName) {
        return userMapper.getUserByUserName(userName);
    }

    @Override
    public List<Role> getRoles() {
        return roleMapper.getRoles();
    }

    @Transactional
    @Override
    public void deleteUser(int uid) {
        userMapper.deleteuser(uid);
        userMapper.deleteuserrole(uid);
    }

    @Transactional
    @Override
    public void addUser(User user, String[] roleNames) {
        userMapper.adduser(user);
        List<String> roleNameList = Arrays.asList(roleNames);
        roleNameList.stream().forEach(roleName -> {
            User_role ur = new User_role();
            Role role = roleMapper.getRoleidbyName(roleName);
            ur.setRole(role);
            ur.setUser(user);
            roleMapper.addUserRole(ur);
        });

    }

    @Override
    public void addUser(User user) {
        userMapper.adduser(user);
    }

    @Transactional
    @Override
    public void updateUser(int uid, User user, String[] rolenames) {
        if (rolenames == null) {
            user.setUid(uid);
            userMapper.updateByPrimaryKeySelective(user);
            userMapper.deleteuserrole(uid);
        } else {
            user.setUid(uid);
            userMapper.updateByPrimaryKeySelective(user);
            userMapper.deleteuserrole(uid);
            for (String rolename : rolenames) {
                Role role = roleMapper.getRoleidbyName(rolename);
                User_role ur = new User_role();
                ur.setRole(role);
                ur.setUser(user);
                roleMapper.addUserRole(ur);
            }
        }

    }

    @Override
    public List<Role> getpageRoleinfo(int current, int pageSize) {
        Page<User> page = new Page(current, pageSize);
        return roleMapper.getRoleList(page);
    }

    @Override
    public List<Role> getRoleinfo() {
        return roleMapper.getRoleList(null);
    }

    @Override
    public List<Permission> getPermisions() {
        return permissionMapper.getPermissions(null);
    }

    @Transactional
    @Override
    public void addrole(Role role, String[] permissionnames) {
        roleMapper.addRole(role);
        for (String permissionname : permissionnames) {
            Permission p = permissionMapper.getPermissionByname(permissionname);
            RolePermission rp = new RolePermission();
            rp.setRole(role);
            rp.setPermission(p);
            roleMapper.addRolePermission(rp);
        }
    }

    @Transactional
    @Override
    public void deleteRole(int rid) {
        roleMapper.deleterole(rid);
        roleMapper.deleteRolePermission(rid);
        roleMapper.deleteUserRole(rid);
    }

    @Override
    public Role getRoleById(int rid) {
        return roleMapper.getRolebyid(rid);
    }

    @Override
    public void deleteRolePermission(int rid) {
        roleMapper.deleteRolePermission(rid);
    }

    @Transactional
    @Override
    public void updateRole(int rid, String[] permissionnames) {
        Role role = roleMapper.getRolebyid(rid);
        for (String permissionname : permissionnames) {
            Permission p = permissionMapper.getPermissionByname(permissionname);
            RolePermission rp = new RolePermission();
            rp.setRole(role);
            rp.setPermission(p);
            roleMapper.addRolePermission(rp);
        }
    }

    @Override
    public List<Permission> getPagePermisions(int current, int pageSize) {
        Page<User> page = new Page(current, pageSize);
        return permissionMapper.getPermissions(page);
    }

    @Override
    public void addPermission(String permissionname) {
        permissionMapper.addPermission(permissionname);
    }

    @Transactional
    @Override
    public void deletepermission(int pid) {
        permissionMapper.deletePermission(pid);
        permissionMapper.deleteRolePermission(pid);
    }

    @Override
    public List<UserRole> listRolesByUserid(int userid) {
        return userRoleMapper.listUserRoleByUid(userid);
    }

    @Override
    public boolean validatePerssion(String username, String perssionName) {
        User user = systemservice.getUserByUserName(username);
        List<User_role> userroles = user.getUser_roles();
        // 默认没有权限
        boolean flag = false;
        for (int k = 0; k < userroles.size(); k++) {
            User_role ur = userroles.get(k);
            Role r = ur.getRole();
            int roleid = r.getRid();
            Role role = systemservice.getRoleById(roleid);
            // 判断该角色的权限是否包含指定的权限
            List<RolePermission> permissionList = role.getRolePermissions().stream().filter(rolePermission -> {
                Permission permission = rolePermission.getPermission();
                return permission.getPermissionname().equals(perssionName);
            }).collect(Collectors.toList());

            if (CollUtil.isNotEmpty(permissionList)) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public boolean validateRole(String username, String roleName) {
        User user = systemservice.getUserByUserName(username);
        List<User_role> userroles = user.getUser_roles();

        // 默认没有权限
        boolean flag = false;
        for (User_role ur : userroles) {
            Role r = ur.getRole();
            if (r.getRolename().equals("采购经理")) {
                flag = true;
            }
        }
        return flag;
    }
}
